package com.lyw.gulimall.ware.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lyw.common.response.ObjectRestResponse;
import com.lyw.gulimall.ware.vo.SkuHasStockVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.lyw.gulimall.ware.entity.WareSkuEntity;
import com.lyw.gulimall.ware.service.WareSkuService;
import com.lyw.common.utils.R;



/**
 * 商品库存
 *
 * @author liyawei
 * @email liyawei@gmail.com
 * @date 2022-05-31 12:20:13
 */
@RestController
@RequestMapping("ware/waresku")
public class WareSkuController {
    @Autowired
    private WareSkuService wareSkuService;


    /**
     * 获取当前 sku 是否有库存
     * @param skuIds
     * @return
     */
    @PostMapping("/hasstock")
    public ObjectRestResponse<List<SkuHasStockVo>> getSkuHasStock(@RequestBody List<Long> skuIds) {
        List<SkuHasStockVo> hasStockToList = wareSkuService.getSkuHasStock(skuIds);
        return ObjectRestResponse.ok(hasStockToList);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    // @RequiresPermissions("ware:waresku:list")
    public R list(@RequestParam(value = "page",required = false, defaultValue = "1") Integer page,
                  @RequestParam(value = "limit", required = false, defaultValue = "10") Integer limit,
                  @RequestParam(value = "skuId", required = false) String skuId,
                  @RequestParam(value = "wareId", required = false) String wareId){
        IPage<Map<String, Object>> list = wareSkuService.queryWareskuPage(page, limit, skuId, wareId);

        return R.ok().put("page", list);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    // @RequiresPermissions("ware:waresku:info")
    public R info(@PathVariable("id") Long id){
		WareSkuEntity wareSku = wareSkuService.getById(id);

        return R.ok().put("wareSku", wareSku);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    // @RequiresPermissions("ware:waresku:save")
    public R save(@RequestBody WareSkuEntity wareSku){
		wareSkuService.save(wareSku);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    // @RequiresPermissions("ware:waresku:update")
    public R update(@RequestBody WareSkuEntity wareSku){
		wareSkuService.updateById(wareSku);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    // @RequiresPermissions("ware:waresku:delete")
    public R delete(@RequestBody Long[] ids){
		wareSkuService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
